package cn.cds.withyou.lib.image.impl;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.widget.ImageView;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.assist.ViewScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.imageaware.NonViewAware;
import com.nostra13.universalimageloader.utils.MemoryCacheUtils;

import java.util.List;

import cn.cds.withyou.lib.image.ImageCache;
import cn.cds.withyou.lib.image.ImageDownloader;
import cn.cds.withyou.lib.image.ImageListener;
import cn.cds.withyou.lib.image.ImageLoader;
import cn.cds.withyou.lib.image.SSLCertificateHandler;

public class UilImageLoader
        implements ImageLoader
{
    private SparseArray<DisplayImageOptions> mDisplayImageOptions = new SparseArray();
    private SparseArray<DisplayImageOptions> mDisplayImageOptionsWithRound = new SparseArray();

    public UilImageLoader(Context context)
    {
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .threadPriority(3)
                .defaultDisplayImageOptions(getDefaultDisplayOptions())
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileCount(200)
                .memoryCacheSizePercentage(10)
                .diskCacheSize(31457280)
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .build();
        com.nostra13.universalimageloader.core.ImageLoader.getInstance().init(config);
        SSLCertificateHandler.nuke();
    }

    private DisplayImageOptions getDefaultDisplayOptions()
    {
        return new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)
                .considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565).cacheInMemory(true).cacheOnDisk(true).build();
    }

    private DisplayImageOptions getDisplayOptions(Drawable drawable)
    {
        return new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)
                .considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565).cacheInMemory(true).cacheOnDisk(true).showImageForEmptyUri(drawable).showImageOnFail(drawable).showImageOnLoading(drawable).build();
    }

    private DisplayImageOptions getDisplayOptions(int defaultImageResId)
    {
        DisplayImageOptions imageOptions = (DisplayImageOptions)this.mDisplayImageOptions.get(defaultImageResId);
        if (imageOptions != null) {
            return imageOptions;
        }
        imageOptions = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)
                .considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565).cacheInMemory(true).cacheOnDisk(true).showImageForEmptyUri(defaultImageResId).showImageOnFail(defaultImageResId).showImageOnLoading(defaultImageResId).build();

        this.mDisplayImageOptions.put(defaultImageResId, imageOptions);
        return imageOptions;
    }

    private DisplayImageOptions getDisplayOptionsWithRound(int defaultImageResId)
    {
        DisplayImageOptions imageOptions = (DisplayImageOptions)this.mDisplayImageOptionsWithRound.get(defaultImageResId);
        if (imageOptions != null) {
            return imageOptions;
        }
        imageOptions = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)
                .considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565)
                .showImageForEmptyUri(defaultImageResId).showImageOnFail(defaultImageResId)
                .showImageOnLoading(defaultImageResId).cacheInMemory(true).cacheOnDisk(true)
                .displayer(new RoundedBitmapDisplayer(25)).build();

        this.mDisplayImageOptionsWithRound.put(defaultImageResId, imageOptions);
        return imageOptions;
    }

    public void setImageCache(ImageCache cache) {}

    public void setImageDownloader(ImageDownloader downloader) {}

    public Bitmap getCache(String uri)
    {
        List<Bitmap> cacheBitmaps = MemoryCacheUtils.findCachedBitmapsForImageUri(
                uri, com.nostra13.universalimageloader.core.ImageLoader.getInstance().getMemoryCache());
        if ((cacheBitmaps != null) && (cacheBitmaps.size() > 0)) {
            return (Bitmap)cacheBitmaps.get(0);
        }
        return null;
    }

    public void loadImage(String uri, ImageListener listener)
    {
        com.nostra13.universalimageloader.core.ImageLoader.getInstance().loadImage(
                uri, UilImageLoaderAdapter.converseTo(listener));
    }

    public Bitmap loadImageSync(String uri)
    {
        return com.nostra13.universalimageloader.core.ImageLoader.getInstance().loadImageSync(uri);
    }

    public void loadAndShowImage(String uri, ImageView view, ImageListener listener)
    {
        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(
                uri, view, UilImageLoaderAdapter.converseTo(listener));
    }

    public void loadAndShowImage(String uri, ImageView view, int defaultImageResId, ImageListener listener)
    {
        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(
                uri, view, getDisplayOptions(defaultImageResId), UilImageLoaderAdapter.converseTo(listener));
    }

    public void loadAndShowImage(String uri, ImageView view, Drawable defaultImageDrawable, ImageListener listener)
    {
        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(
                uri, view, getDisplayOptions(defaultImageDrawable), UilImageLoaderAdapter.converseTo(listener));
    }

    public void loadAndShowImage(String uri, ImageView view, boolean needRound, int defaultImageResId, ImageListener listener)
    {
        if (needRound) {
            com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(
                    uri, view, getDisplayOptionsWithRound(defaultImageResId), UilImageLoaderAdapter.converseTo(listener));
        } else {
            loadAndShowImage(uri, view, defaultImageResId, listener);
        }
    }

    public void loadAndShowImage(String uri, ImageView view, int defaultImageResId, int maxWidth, int maxHeight, ImageListener listener)
    {
        ImageSize targetImageSize = new ImageSize(maxWidth, maxHeight);
        NonViewAware imageAware = new ResizeImageAware(view, uri, targetImageSize, ViewScaleType.CROP);
        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(
                uri, imageAware, getDisplayOptions(defaultImageResId), UilImageLoaderAdapter.converseTo(listener));
    }

    public void pause()
    {
        com.nostra13.universalimageloader.core.ImageLoader.getInstance().pause();
    }

    public void onResume()
    {
        com.nostra13.universalimageloader.core.ImageLoader.getInstance().resume();
    }
}
